﻿Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes

Partial Public Class SilverPlayerInfo
  Inherits UserControl

  Private player As Engine.Player

  ''' <summary>
  ''' constructor
  ''' </summary>
  ''' <param name="player">player this panel represents</param>
  ''' <remarks></remarks>
  Public Sub New(ByVal player As Engine.Player)
    ' Required to initialize variables
    InitializeComponent()

    Me.player = player
    TextBlockPlayerName.Text = Me.player.Name
    AddHandler player.ScoreChanged, AddressOf cardsWon
    AddHandler player.TurnStateChanged, AddressOf stateChanged

    'correct color
    syncPlayerState()
    'reset score
    syncScore()
  End Sub

  ''' <summary>
  ''' change score indicator when cardswon property changes
  ''' </summary>
  Private Sub cardsWon(ByVal sender As Object, ByVal e As EventArgs)
    Me.Dispatcher.BeginInvoke(AddressOf syncScore)
  End Sub

  Private Sub stateChanged(ByVal sender As Object, ByVal e As EventArgs)
    'can be a cross-thread event
    Me.Dispatcher.BeginInvoke(AddressOf syncPlayerState)
  End Sub

  ''' <summary>
  ''' sync colors to represent the correct player state.
  ''' </summary>
  Private Sub syncPlayerState()
    'reset panes
    BorderGreenPane.Visibility = Windows.Visibility.Collapsed
    BorderRedPane.Visibility = Windows.Visibility.Collapsed
    BorderGoldPane.Visibility = Windows.Visibility.Collapsed
    '(BorderBluepane is the bottom-most pane, can always be visible as fall-back)
    Select Case player.TurnState
      Case Engine.Player.TurnStateE.MayMakeFirstMove, Engine.Player.TurnStateE.MayMakeSecondMove
        BorderGoldPane.Visibility = Windows.Visibility.Visible
      Case Engine.Player.TurnStateE.LoosesTurn
        BorderRedPane.Visibility = Windows.Visibility.Visible
      Case Engine.Player.TurnStateE.WinsTurn
        BorderGreenPane.Visibility = Windows.Visibility.Visible
    End Select
  End Sub

  ''' <summary>
  ''' set score
  ''' </summary>
  Private Sub syncScore()
    TextBlockScore.Text = player.Score.ToString()
  End Sub

End Class
